// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import common from './components/common'
import filter from './filter'
import store from './store/'
import baseService from './service/index'
import plugins from './plugins/'
import config from './config'
import refresh from './refresh/refresh'
import MintUI from 'mint-ui'
import FastClick from './plugins/fastclick'
import baseService1 from './service/base.service1.js'
import particles from 'particles.js'
import TWEEN from '@tweenjs/tween.js'
import echarts from 'echarts'

import './assets/transition.less'
import './assets/theme.less'
import 'mint-ui/lib/style.css'
import 'echarts/lib/chart/line'
import 'echarts/lib/component/graphic'
import 'echarts/lib/component/tooltip'
import 'echarts/lib/component/title'
import './assets/wonderland'

Vue.prototype.$axios = axios
Vue.config.productionTip = false
Vue.prototype.baseService = baseService
Vue.prototype.$plugins = plugins
Vue.prototype.$refreshToken = refresh
Vue.prototype.$config = config
Vue.prototype.$http = baseService1.baseService1
window.TWEEN = TWEEN
window.echarts = echarts

Object.keys(common).forEach((key) => {
  var name = key.replace(/(\w)/, (v) => v.toUpperCase())
  Vue.component(`v${name}`, common[key])
})

Object.keys(filter).forEach((key) => {
  var name = key.replace(/(\w)/, (v) => v.toUpperCase())
  Vue.filter(`v${name}`, filter[key])
})

router.beforeEach(({meta, path}, from, next) => {
  store.state.networkState = navigator.onLine
  let hasIntercept = false
  config.routeIntercept.forEach((item) => {
    if (item === path) {
      hasIntercept = true
    }
  })
  if (hasIntercept) {
    let { auth = true } = meta
    let isLogin = typeof store.state.user
    if (auth && isLogin === 'object' && path !== '/user/login') {
      return next({ path: '/login' })
    }
  }
  if (path === '/qstrategy') {
    return next('/qstrategy/' + store.state.strategyRouterState)
  }
  next()
})

if ('addEventListener' in document) {
  document.addEventListener('DOMContentLoaded', function() {
    FastClick.attach(document.body);
  }, false);
}

Vue.use(MintUI)
Vue.use(particles)

new Vue({
  render: h => h(App),
  router,
  axios,
  store,
  components: { App }
}).$mount("#app");
